from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class alertPage:
    def __init__(self, driver):
        self.driver = driver
        self.alert_button = (By.XPATH, "/html/body/div[4]/form/input[2]")  # 假设点击按钮的元素定位

    def click_alert_button(self):
        """点击触发弹窗的按钮"""
        self.driver.find_element(*self.alert_button).click()

    def get_alert_text(self):
        """获取弹窗中的文本"""
        # 等待弹窗出现
        WebDriverWait(self.driver, 10).until(EC.alert_is_present())
        alert = Alert(self.driver)  # 创建 Alert 对象
        return alert.text  # 获取弹窗的文本

    def accept_alert(self):
        """接受弹窗 (点击确定按钮)"""
        alert = Alert(self.driver)
        alert.accept()

    def dismiss_alert(self):
        """拒绝弹窗 (点击取消按钮)"""
        alert = Alert(self.driver)
        alert.dismiss()
